package com.threeti.jiayusms.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.threeti.jiayusms.domain.model.CommunicationRule;
import com.threeti.jiayusms.domain.service.communicationrule.CommunicationRuleQueryService;
import com.threeti.jiayusms.domain.service.communicationrule.CommunicationRuleUpdateService;
import com.threeti.jiayusms.domain.util.TemplateIdGenerator;
import com.threeti.jiayusms.framework.util.DynaSearchCriteria;
import com.threeti.jiayusms.framework.util.PagedListHandler;
import com.threeti.jiayusms.framework.web.PagedQueryControllerAdapter;
import com.threeti.jiayusms.web.form.RuleTemplateForm;

@Controller
public class RuleTemplateMainController {

	@Autowired
	private CommunicationRuleQueryService queryService;
	
	@Autowired
	private CommunicationRuleUpdateService updateService;
	
	@RequestMapping(value="/ruleTemplateMain.html")
	  public String enterRuleTemplateMain(HttpServletRequest request,Map modelMap) {
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationRule> pagedRuleTemplates = queryService.findAllRuleTemplatesByPage(index[0], index[1]);
	    pagedHelper.processPage(pagedRuleTemplates, request);
	    
	    modelMap.put("ruleTemplates", pagedRuleTemplates.getPageList());
	    
	    return "admin/ruleTemplateMain";
	  }
	
	
	 @RequestMapping(value="/newRuleTemplate.html", method=RequestMethod.POST)
	  public String newMsgTemplate(@ModelAttribute RuleTemplateForm form) {
	  	CommunicationRule ruleTemplate = new CommunicationRule();
	  	
	  	ruleTemplate.setDescription(form.getDescription());
	  	ruleTemplate.setSendCount(form.getSendCount());
	  	ruleTemplate.setSendInterval(form.getSendInterval());
  	
	  	CommunicationRule rtnRuleTemplate = updateService.storeCommunicationRule(ruleTemplate);
	  	String templateId = TemplateIdGenerator.generateTempalteIdBy("GZ", rtnRuleTemplate);
	  	rtnRuleTemplate.setCustomId(templateId);
	  	updateService.storeCommunicationRule(rtnRuleTemplate);
	    
	    return "redirect:ruleTemplateMain.html";
	  }
	 
	 @RequestMapping(value="/removeRuleTemplate.html", method = RequestMethod.POST)
	  public String removeRuleTemplate(@RequestParam("keys") String pks,
	                            Map modelMap, HttpServletRequest request) {
	    String[] ruleTemplateIds = pks.split(",");

	    for (int i = 0; i < ruleTemplateIds.length; i++) {
	      updateService.removeCommunicationRuleBy(Long.valueOf(ruleTemplateIds[i]));
	    }
	    return "redirect:/ruleTemplateMain.html";
	  }
	 
	  @RequestMapping(value="/enterRuleTemplateEditForm.html", method = RequestMethod.GET)
	  public String enterEditRuleTemplate(@RequestParam("updatekey") String ruleTemplateId,
	                            Map modelMap, HttpServletRequest request) {
	  	CommunicationRule template = queryService.findCommunicationRuleBy(Long.valueOf(ruleTemplateId));
	  	
	  	modelMap.put("ruleTemplate", template);
	    
	    return "admin/editRuleTemplateForm";
	  }
	  
	  @RequestMapping(value="/editRuleTemplate.html", method = RequestMethod.POST)
	  public String editRuleTemplate(@ModelAttribute RuleTemplateForm form,
	                            Map modelMap, HttpServletRequest request) {
	  	CommunicationRule template = queryService.findCommunicationRuleBy(Long.valueOf(form.getUpdatekey()));
	  	
	  	template.setDescription(form.getDescription());
	  	template.setSendCount(form.getSendCount());
	  	template.setSendInterval(form.getSendInterval());
  	
	  	updateService.storeCommunicationRule(template);
	  	
	  	modelMap.put("ruleTemplate", template);
	    
	    return "redirect:/ruleTemplateMain.html";
	  }
	  
	  @RequestMapping(value="/searchRuleTemplateByPage.html")
	  public String searchRuleTemplate(HttpServletRequest request, @ModelAttribute RuleTemplateForm form, Map modelMap) {

	  	DynaSearchCriteria dynaSearchCriteria = null;
	  	
	    PagedQueryControllerAdapter pagedHelper = new PagedQueryControllerAdapter();
	  	if (pagedHelper.isAnyPageSearch(request)) {
	  		dynaSearchCriteria = (DynaSearchCriteria)modelMap.get(RuleTemplateForm.SESSION_DYNA_CRITERIA_KEY);
	  	} else {
	      modelMap.put(RuleTemplateForm.SESSION_DYNA_CRITERIA_KEY, (dynaSearchCriteria = form.buildDynaSearchCriteriaBy("com.threeti.jiayusms.interfaces.RuleTemplateDynaCriteriaDto")));  		
	  	}
	    int[] index = pagedHelper.getIndex(request);    
	    PagedListHandler<CommunicationRule> pagedRuleTemplates = queryService.findRuleTemplatesByPage(dynaSearchCriteria, index[0], index[1]);
	    pagedHelper.processPage(pagedRuleTemplates, request);
	    
	    modelMap.put("ruleTemplates", pagedRuleTemplates.getPageList());
	    
	    return "admin/ruleTemplateMain";
	  }

	
}
